﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;

namespace FrameWork.web.Module.ITMS.KnowledgeBase.KBType
{
    public partial class Edit : System.Web.UI.Page
    {
        int KID = (int)Common.sink("KID", MethodType.Get, 255, 1, DataType.Int);
        string CMD = (string)Common.sink("CMD", MethodType.Get, 255, 0, DataType.Str);

        protected void Page_Load(object sender, EventArgs e)
        {
            BindButton();
            if (!Page.IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            its_KnowledgeBaseKind gt = BusinessFacade.its_KnowledgeBaseKindDisp(KID);
            //sys_GroupTable gt = BusinessFacade.sys_GroupDisp(GroupID);
            this.G_CName.Text = gt.K_KName;
            if (gt.K_ParentID == 0)
                G_ParentID_Txt.Text = "类别列表";
            else
                G_ParentID_Txt.Text = BusinessFacade.its_KnowledgeBaseKindDisp(gt.K_ParentID).K_KName;
            CatListTitle.Text = string.Format("<a href='KindList.aspx'>类别列表</a>{0}", BusinessFacade.GetKindTitle(KID));
            //判断是否删除
            if (CMD == "Delete")
            {
                FrameWorkPermission.CheckPermissionVoid(PopedomType.Delete);

                //删除子分类
                DeleteCat(KID);
                //删除当前分类
                BusinessFacade.Update_Table_Fileds("its_KnowledgeBaseKind", "K_Delete=1", string.Format("KID={0}", KID));


                if (gt.K_ParentID != 0)
                {
                    //更新父类子类数
                    BusinessFacade.Update_Table_Fileds("its_KnowledgeBaseKind", "K_ChildCount=K_ChildCount-1", string.Format("KID={0}", gt.K_ParentID));
                    //更新父类子类排序  
                    int RecordCount = 0;
                    QueryParam qp = new QueryParam();
                    qp.Where = string.Format("Where K_ParentID={0} and K_Delete=0", gt.K_ParentID);
                    qp.Orderfld = "K_Level,K_ShowOrder";
                    qp.OrderType = 0;
                    ArrayList lst = BusinessFacade.its_KnowledgeBaseKindList(qp, out RecordCount);
                    RecordCount = 1;
                    foreach (its_KnowledgeBaseKind var in lst)
                    {
                        BusinessFacade.Update_Table_Fileds("its_KnowledgeBaseKind", string.Format("K_ShowOrder={0}", RecordCount), string.Format("KID={0}", var.KID));
                        RecordCount++;
                    }

                }
                EventMessage.MessageBox(1, "操作成功", string.Format("删除类别({0})成功!", gt.K_KName), Icon_Type.OK, Common.GetHomeBaseUrl("KindList.aspx"), Common.BuildJs);

            }
        }

        private void BindButton()
        {
            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.Delete;
            bi1.ButtonUrl = string.Format("DelData('?CMD=Delete&KID={0}')", KID);
            bi1.ButtonUrlType = UrlType.JavaScript;
            HeadMenuWebControls1.ButtonList.Add(bi1);

            HeadMenuButtonItem bi0 = new HeadMenuButtonItem();
            bi0.ButtonIcon = "back.gif";
            bi0.ButtonName = "返回";
            bi0.ButtonPopedom = PopedomType.List;
            bi0.ButtonUrl = string.Format("KindList.aspx?KID={0}", KID);
            HeadMenuWebControls1.ButtonList.Add(bi0);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FrameWorkPermission.CheckPermissionVoid(PopedomType.Edit);
            its_KnowledgeBaseKind gt = BusinessFacade.its_KnowledgeBaseKindDisp(KID);
            gt.K_KName = (string)Common.sink(G_CName.UniqueID, MethodType.Post, 50, 1, DataType.Str);
            gt.DB_Option_Action_ = "Update";
            BusinessFacade.its_KnowledgeBaseKindInsertUpdate(gt);
            EventMessage.MessageBox(1, "操作成功", string.Format("修改类别ID({0})成功!", gt.KID), Icon_Type.OK, Common.GetHomeBaseUrl(string.Format("KindList.aspx?KID={0}", KID)), Common.BuildJs);
        }

        private void DeleteCat(int KID)
        {
            //删除子分类和删除当前分类
            int RecordCount = 0;
            QueryParam qp = new QueryParam();
            qp.Where = string.Format("Where K_ParentID={0} and K_Delete = 0 ", KID);
            ArrayList lst = BusinessFacade.its_KnowledgeBaseKindList(qp, out RecordCount);
            foreach (its_KnowledgeBaseKind var in lst)
            {
                BusinessFacade.Update_Table_Fileds("its_KnowledgeBaseKind", "K_Delete=1", string.Format("KID={0}", var.KID));
                DeleteCat(var.KID);
            }
        }

    }
}
